package pers.xuehongyan.useat.server.auth;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;

/**
 * @author xuehongyan
 */
public class JwtUtils {
    final static String DEFAULT_SECRET = "TS2XI2YN3K4JZ5DXD8YA9JJH5PM1IA6HX7CX7Q3JO";
    final static String ISSUER = "xuehongyan_jwt";

    public static String encode(String key, String value, long expMillis) {
        Date expDate = new Date(System.currentTimeMillis() + expMillis);
        Algorithm algorithm = Algorithm.HMAC256(DEFAULT_SECRET);
        return JWT.create()
                .withIssuer(ISSUER)
                .withClaim(key, value)
//                .withExpiresAt(expDate)
                .sign(algorithm);

    }

    public static String decode(String token, String key) {
        return JWT.decode(token).getClaim(key).asString();
    }
}
